#!/bin/sh

test_description="test bad or special pktline format"

. ./lib/sharness.sh

test_expect_success "create pipe" '
        mkfifo pipe
'
 
do_receive_pack_1 () {
	output="0055version 200\n" && printf >&2 "> $output" && printf "$output" && exit
}

test_expect_failure "short read, too early EOF" '
        (
		do_receive_pack_1 <pipe | \
                test_must_fail proc-receive --mock >pipe
	) >actual 2>&1 &&
        cat >expect <<-EOF &&
	> 0055version 200
	proc-receive: read error
	EOF
	test_cmp expect actual
'


do_receive_pack_2 () {
	output="version 200\n" &&
	printf >&2 "> $output" &&
	printf "$output" &&
	exit
}

test_expect_failure "bad line length character" '
        (
		do_receive_pack_2 <pipe | \
                test_must_fail proc-receive --mock >pipe
	) >actual 2>&1 &&
        cat >expect <<-EOF &&
	> version 200
	proc-receive: protocol error: bad line length character: vers
	EOF
	test_cmp expect actual
'

test_expect_failure "special packet: 0000" '
        (
		printf "0000" | \
                test_must_fail proc-receive --mock
	) >actual 2>&1 &&
        cat >expect <<-EOF &&
	EOF
	test_cmp expect actual
'

test_expect_failure "special packet: 0001" '
        (
		printf "0001" | \
                test_must_fail proc-receive --mock
	) >actual 2>&1 &&
        cat >expect <<-EOF &&
Invalid packet length.
EOF
	test_cmp expect actual
'

test_expect_failure "special packet: 0002" '
        (
		printf "0002" | \
                test_must_fail proc-receive --mock
	) >actual 2>&1 &&
        cat >expect <<-EOF &&
Invalid packet length.
EOF
	test_cmp expect actual
'

test_expect_failure "special packet: 0003" '
        (
		printf "0003" | \
                test_must_fail proc-receive --mock
	) >actual 2>&1 &&
        cat >expect <<-EOF &&
Invalid packet length.
EOF
	test_cmp expect actual
'

test_expect_failure "special packet: 0004" '
        (
		printf "0004" | \
                test_must_fail proc-receive --mock
	) >actual 2>&1 &&
        cat >expect <<-EOF &&
Invalid packet length.
EOF
	test_cmp expect actual
'

test_done
